﻿using brevis.Nop.SDK.Entity;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;

namespace brevis.Nop.BusinessLogic.Data
{
    public class brevisNopDataManager
    {
        /// <summary>
        /// DB context field
        /// </summary>
        private static brevisNopModelDataContext _db;

        /// <summary>
        /// DB context property
        /// </summary>
        public static brevisNopModelDataContext DB 
        {
            get
            {
                if (_db == null)
                {
                    _db = new brevisNopModelDataContext(
                        ConfigurationManager.ConnectionStrings["NopSqlConnection"].ConnectionString);
                }

                return _db;
            }
            set
            {
                _db = value;
            }
        }

        /// <summary>
        /// Gets a report entity instance
        /// </summary>
        /// <param name="reportid">Report Id</param>
        /// <returns>Report entity object</returns>
        public static NopReport NopReport_Get(string reportid)
        {
            try
            {
                var ret = DB.Nop_Reports
                    .Where(r => r.ReportId.Equals(reportid))
                    .Select(r => r)
                    .FirstOrDefault();

                if (ret == null)
                    throw new NullReferenceException(
                        string.Format("report [{0}] not found. please check key.", reportid));

                return brevisNopDataMapper.MapToNopReport(ret);

            }
            catch (Exception err)
            {

                throw err;
            }
            finally
            {
                DB = null;
            }

        }

    }
}
